<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="hAdd(0,1)">添加权限</el-button>
      <el-dialog :visible="showDialog" :title="isEdit?'修改权限': '新增权限'" width="500px" @close="hClose">
        <el-form ref="forData" label-width="80px" :model="dataObj" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="dataObj.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="dataObj.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="dataObj.description" type="textarea" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="dataObj.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="hSave">确定</el-button>
            <el-button @click="hClose">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-table :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="hAdd(row.id,2)">添加</el-button>
            <el-button size="mini" type="text" @click="hEdit(row.id)">编辑</el-button>
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗"
              @onConfirm="hDel(row.id)"
            >
              <el-button slot="reference" size="mini" type="text" style="margin-left:10px">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getPermission, addPermission, delPermission, permissionDetail, editPermission } from '@/api/permission'
import { arr2tree } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      isEdit: false,
      showDialog: false,
      dataObj: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: ''
      },
      rules: {
        name: [
          { required: true, message: '权限名称必填', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识必填', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述必填', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermission()
      this.list = arr2tree(res.data, 0)
    },
    hAdd(pid, type) {
      this.isEdit = false
      this.dataObj.pid = pid
      this.dataObj.type = type
      this.showDialog = true
    },
    hClose() {
      this.showDialog = false
      this.$refs.forData.resetFields()
    },
    hSave() {
      this.$refs.forData.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async doAdd() {
      await addPermission(this.dataObj)
      this.$message.success('添加成功')
      this.getPermissionList()
      this.hClose()
    },
    async  hDel(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    },
    async hEdit(id) {
      this.isEdit = true
      const res = await permissionDetail(id)
      this.showDialog = true
      this.dataObj = res.data
    },
    async doEdit() {
      await editPermission(this.dataObj)
      this.$message.success('修改成功')
      this.getPermissionList()
      this.hClose()
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
